<?php
/* This file is part of LlamaSlayers Ajax Chat
 *
 * The source code is released under the GNU GPL free software license, version 3.
 * Updates can be found at http://llamaslayers-ajax-chat.googlecode.com/
 */

require_once dirname( __FILE__ ) . '/config.php';

session_start();

if ( !empty( $_SESSION['name'] ) ) {
	header( 'Location: ' . CHAT_HOME . 'chat.php' );
	exit;
}

if ( !isset( $_GET['name'] ) )
	$_GET['name'] = '';
$_GET['name'] = trim( $_GET['name'] );

foreach ( str_split( $_GET['name'] ) as $n ) {
	if ( in_array( $n, str_split( 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123546789-_ ~`' ) ) )
		continue;
	header( 'Location: ' . CHAT_HOME . 'chat.php' );
	exit;
}

if ( file_exists( dirname( __FILE__ ) . '/db/user/' . strtolower( $_GET['name'] ) . '.txt' ) ) {
	if ( empty( $_POST['pass'] ) ) { ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-type" content="text/html;charset=UTF-8" /> 
	<title>Username reserved</title>
	<link href="style.css" rel="stylesheet" type="text/css"/>
</head>
<body>
	<h1>Username reserved</h1>
	<p>The username you tried to log in with is reserved. If you are the owner, you can log in below. Otherwise, you'll need to go <a href="chat.php">back to the chat</a> and try a different name. Cookies are required to log in.</p>
	<form action="name-noscript.php?name=<?php echo urlencode( $_GET['name'] ); ?>" method="post">
		<label for="pass">Password:</label> <input type="password" id="pass" name="pass"/>
	</form>
</body>
</html>
<?php
		exit;
	} else {
		$user = file( dirname( __FILE__ ) . '/db/user/' . strtolower( $_GET['name'] ) .'.txt' );
		if ( base64_encode( superhash( $_POST['pass'], $user[0] ) ) == $user[0] ) {
			$_SESSION['name'] = $_GET['name'];
			$_SESSION['authenticated'] = strtolower( $_GET['name'] );
			header( 'Location: ' . CHAT_HOME . 'chat.php' );
			exit;
		} ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-type" content="text/html;charset=UTF-8" /> 
	<title>Username reserved</title>
	<link href="style.css" rel="stylesheet" type="text/css"/>
</head>
<body>
	<h1>Username reserved</h1>
	<p>The username you tried to log in with is reserved. If you are the owner, you can log in below. Otherwise, you'll need to go <a href="chat.php">back to the chat</a> and try a different name. Cookies are required to log in.</p>
	<p><strong>Your previous login attempt failed due to an incorrect password.</strong></p>
	<form action="name-noscript.php?name=<?php echo urlencode( $_GET['name'] ); ?>" method="post">
		<label for="pass">Password:</label> <input type="password" id="pass" name="pass"/>
	</form>
</body>
</html>
<?php
		exit;
	}
}

if ( in_array( strtolower( $_GET['name'] ), (array)$block_username_registration ) || strlen( strtolower( $_GET['name'] ) ) < MIN_USERNAME_LENGTH ) {
	header( 'Location: ' . CHAT_HOME . 'chat.php' );
	exit;
}

if ( !empty( $_GET['name'] ) ) {
	$_SESSION['name'] = $_GET['name'];
	header( 'Location: ' . CHAT_HOME . 'chat.php' . ( empty( $_COOKIE ) ? '?name=' . urlencode( $_GET['name'] ) : '' ) );
} else {
	header( 'Location: ' . CHAT_HOME . 'chat.php' );
}


?>